package com.liu.controller;

import com.liu.bean.LoginBean;
import com.liu.bean.ResultBean;
import com.liu.bean.User;
import com.liu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@CrossOrigin //跨域处理
public class UserController extends BaseController {

    @Autowired
    UserService userService;


    @PostMapping("/user")
    public ResultBean save(@RequestBody User user) {
        int rs = userService.save(user);
        return rs == 1 ? success("保存成功") : fail("保存失败");
    }

    @PostMapping("/user/login")
    public ResultBean login(@RequestBody LoginBean loginBean) {
        User user = userService.queryByUsername(loginBean.getUsername());
        if (null== user){
            return fail("用户不存在");
        }
        //判断密码
        String tmp= DigestUtils.md5DigestAsHex(loginBean.getPassword().getBytes());
        if (!tmp.equals(user.getPassword())){
            return fail("密码错误");
        }
        //判断状态
        if (user.getStatus()==0){
            return fail("用户被禁用");
        }


        return success("登陆成功",user);
    }



}
